package token;

import LandR.login.loginSrc.function.User.User;

import java.sql.*;
import java.util.UUID;

public class TokenHelp {
    /**
     * 随机获取一个token
     *
     * @return
     */
    public String gainToken() {
        String token = String.valueOf(UUID.randomUUID());
        return token;
    }

    /**
     * 更新users数据表中的token
     *
     * @param id
     * @param token
     */
    public void updateTokenForUsers(int id, String token) throws SQLException {
        User loginUser = new User();
        loginUser.setId(id);

        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update users set Token=? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);

            ppst.setString(1, token);
            ppst.setInt(2, id);

            ppst.executeUpdate();
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        } finally {
            ppst.close();
            conn.close();
        }

    }

    /**
     * 更新usersdata数据表中的数据
     *
     * @param id
     * @param token
     * @throws SQLException
     */
    public void updateTokenForUsersData(int id, String token) throws SQLException {
        LandR.personal.personalHelp.Users.User user = new LandR.personal.personalHelp.Users.User();
        user.setId(id);
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update usersdata set Token=? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);

            ppst.setString(1, token);
            ppst.setInt(2, id);

            ppst.executeUpdate();
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        } finally {
            ppst.close();
            conn.close();
        }


    }

    public int judgeToken(String token) throws SQLException {
        String sql = "select * from usersdata where Token = ?";
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rs = null;
        Integer id = -1;
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, token);
            rs = (ResultSet) ppst.executeQuery();
            while (rs.next()) {
                id = rs.getInt("id");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            rs.close();
            ppst.close();
            conn.close();
        }
        return id;
    }

    public Long selectThreadID(String token) {
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rs = null;
        String sql = "select * from users where token = ?";
        Long threadid = null;
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, token);
            rs = (ResultSet) ppst.executeQuery();
            while (rs.next()) {
                threadid = rs.getLong("thread_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(rs);
            close(ppst);
            close(conn);
        }
        return threadid;
    }

    public void updateThreadIDForUsers(int id, Long threadID) throws SQLException {
        Connection conn = null;
        PreparedStatement ppst = null;
        System.out.println("线程名为" + threadID);
        System.out.println("用户id为：" + id);
        String sql = "update users set thread_id = ? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);

            ppst.setLong(1, threadID);
            ppst.setInt(2, id);
            ppst.executeUpdate();
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        } finally {
            ppst.close();
            conn.close();
        }


    }


    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        final String URL = "jdbc:mysql://123.57.3.195:3306/YDYP?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
        final String username = "root";
        final String password = "ZAMTYR1314";
        Connection conn = DriverManager.getConnection(URL, "root", "ZAMTYR1314");
        if (conn == null) {
            System.out.println("创建连接失败");
            close(conn);
            return null;
        } else {
            System.out.println("创建连接成功");
            return conn;
        }

    }

    //2.getPrepareStatement  获取PrepareStatement接口方法
    public static PreparedStatement getPrepareStatement(String sql) throws SQLException, ClassNotFoundException {
        Connection conn = getConnection();
        PreparedStatement ppsm = (PreparedStatement) conn.prepareStatement(sql);
        return ppsm;
    }

    //3.关闭prepareStatement接口的方法
    public static void close(PreparedStatement pstmt) {
        if (pstmt != null) {                        //避免出现空指针异常
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    //4.关闭connection接口的方法
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //5.关闭resultSet接口的方法
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }


    }

    public void dropTokenForUsers(int ID) {
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update users set Token = ? where id = ?";
        System.out.println(ID);
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, "过期");
            ppst.setInt(2, ID);
            ppst.execute();
            System.out.println("token过期成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

        }

    }

    public void dropTokenForUsersdata(int ID) {
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update usersdata set Token = ? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, "过期");
            ppst.setInt(2, ID);
            ppst.execute();
            System.out.println("token过期成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

        }

    }

    public void updateTokenForAllNotes(int id, String token) throws SQLException {
        LandR.personal.personalHelp.Users.User user = new LandR.personal.personalHelp.Users.User();
        user.setId(id);
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update allnotes set token=? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);

            ppst.setString(1, token);
            ppst.setInt(2, id);

            ppst.executeUpdate();
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        } finally {
            ppst.close();
            conn.close();
        }
    }

    public void dropTokenForAllnotes(int ID) {
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update allnotes set token = ? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, "过期");
            ppst.setInt(2, ID);
            ppst.execute();
            System.out.println("token过期成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

        }
    }


}